#include "UIObject.h"

#include "../PLATFORM.h"
#ifdef _ANDROID_PLATFORM__
#include "SDL_log.h"
#else
#include <SDL2/SDL_log.h>
#endif

UIObject::UIObject(std::string uiObjectId, const int x, const int y,
                   const int width, const int height, double scale,
                   double angle)
    : uiObjectId(uiObjectId),
      x(x),
      y(y),
      width(width),
      height(height),
      scale(scale),
      angle(angle),
      contentWidth(width),
      contentHeight(height),
      contentX(0),
      contentY(0) {}

void UIObject::addChild(UIObject* child) {
  if (!child_objects.hasKey(child->uiObjectId)) {
    if (child->parent_object == nullptr) {
      //如果不存在，且child的父对象为空时,则添加
      child_objects.push_back(child->uiObjectId, child);
      child->parent_object = this;  //并且将子对象的父对象设为当前对象
    } else {
      // child的父对象不为空时
      SDL_LogError(0,
                   "对象(id:%s)已经绑定了其他父对象(id:%s),"
                   "不能再绑定到该对象(id:%s)中!",
                   child->uiObjectId.c_str(),
                   child->parent_object->uiObjectId.c_str(),
                   uiObjectId.c_str());
    }

  } else {
    //如果已经存在
    SDL_LogError(0, "对象(id:%s)中已经有对象(id:%s)了", uiObjectId.c_str(),
                 child->uiObjectId.c_str());
  }
}

UIObject* UIObject::getChild(std::string uiObjectId) {
  //如果有这个子对象
  if (child_objects.hasKey(uiObjectId)) {
    return child_objects[uiObjectId];
  } else {
    SDL_LogError(0, "对象(id:%s)中没有这个子对象(id:%s)",
                 UIObject::uiObjectId.c_str(), uiObjectId.c_str());
    // throw;
    return nullptr;
  }
}

void UIObject::init() {
  //设置相对位置
  if (parent_object != nullptr) {
    relativeX = x - parent_object->getX();
    relativeY = y - parent_object->getY();
  } else {
    relativeX = x;
    relativeY = y;
  }

  //子对象的init()
  for (int i = 0; i < child_objects.getSize(); i++) {
    if (child_objects[i].second != nullptr &&
        child_objects[i].second->enable == true) {
      child_objects[i].second->init();
    }
  }

  //先执行一遍绘制
  updateContentData();
  drawArea(Rect{contentX, contentY, contentWidth, contentHeight});
}

void UIObject::event() {
  for (int i = 0; i < child_objects.getSize(); i++) {
    if (child_objects[i].second != nullptr &&
        child_objects[i].second->enable == true) {
      child_objects[i].second->event();
    }
  }
}
void UIObject::updateContentData() {
  setContentXY(0, 0);
  setContentWH(width, height);
}

void UIObject::update() {
  //自动更新相对位置
  if (parent_object != nullptr) {
    relativeX = x - parent_object->getX();
    relativeY = y - parent_object->getY();
  } else {
    relativeX = x;
    relativeY = y;
  }

  //子对象的update()
  for (int i = 0; i < child_objects.getSize(); i++) {
    if (child_objects[i].second != nullptr &&
        child_objects[i].second->enable == true) {
      child_objects[i].second->update();
    }
  }
}

void UIObject::draw() {
  // for (int i = 0; i < child_objects.getSize(); i++) {
  //   //子对象中只要有一个是FILL，那么该对象也是FILL
  //   // if (heightMode != WHMode::FILL) {
  //   //   if (child_objects[i].second->getHeightMode() == WHMode::FILL) {
  //   //     heightMode = WHMode::FILL;
  //   //   }
  //   // }
  //   // if (widthMode != WHMode::FILL) {
  //   //   if (child_objects[i].second->getWidthMode() == WHMode::FILL) {
  //   //     widthMode = WHMode::FILL;
  //   //   }
  //   // }
  //   //如果该对象为FIT,那么如果子对象是FILL,也得改为FIT
  //   if (heightMode == WHMode::FIT) {
  //     if (child_objects[i].second->getHeightMode() == WHMode::FILL) {
  //       child_objects[i].second->setHeightMode(WHMode::FIT);
  //     }
  //   }
  //   // if (widthMode != WHMode::FILL) {
  //   //   if (child_objects[i].second->getWidthMode() == WHMode::FILL) {
  //   //     widthMode = WHMode::FILL;
  //   //   }
  //   // }
  //   if (child_objects[i].second != nullptr &&
  //       child_objects[i].second->enable == true) {
  //     child_objects[i].second->draw();
  //   }
  // }
  drawArea(Rect{x, y, width, height});
}

void UIObject::drawArea(const Rect& area) {
  for (int i = 0; i < child_objects.getSize(); i++) {
    // 如果该对象为FIT,那么如果子对象是FILL,子对象也得改为FIT
    if (heightMode == WHMode::FIT) {
      if (child_objects[i].second->getHeightMode() == WHMode::FILL) {
        child_objects[i].second->setHeightMode(WHMode::FIT);
      }
    }
    //子对象中只要有一个是FILL，那么该对象也是FILL
    if (heightMode != WHMode::FILL) {
      if (child_objects[i].second->getHeightMode() == WHMode::FILL) {
        heightMode = WHMode::FILL;
      }
    }
    if (child_objects[i].second != nullptr &&
        child_objects[i].second->enable == true) {
      UIObject* object = child_objects[i].second;
      // //如果在区域内才进行渲染
      if (!(object->x > area.x + area.width ||
            object->y > area.y + area.height)) {
        object->updateContentData();
        object->drawArea(rectIntersection(
            Rect{object->contentX + object->x, object->contentY + object->y,
                 object->contentWidth, object->contentHeight},
            area));
      }
    }
  }
}

UIObject::~UIObject() {
  for (int i = 0; i < child_objects.getSize(); i++) {
    if (child_objects[i].second != nullptr) {
      delete child_objects[i].second;
      child_objects[i].second = nullptr;
    }
  }
  printf("%s析构完毕！\n", uiObjectId.c_str());
}